package com.tankGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Scanner;
import java.util.Vector;

/**
 * @author LingLed
 * @version 1.0
 */
public class MyPanel extends JPanel implements KeyListener, Runnable {
    public static final int WIDTH = 1000;
    public static final int HEIGHT = 750;
    ////我的坦克
    private Hero hero;

    //敌人坦克,考虑到后期多线程问题,使用Vector
    Vector<Tank> tanks = Enemy.tanks;

    private int enemySize = 3;

    public MyPanel() {
        hero = new Hero(700, 650, Tank.TYPE_PLAYER);
        Tank.tanks.add(hero);
        for (int i = 0; i < enemySize; i++) {
            tanks.add(new Enemy(200 + i * 100, 0));
        }
        for (int i = 0; i < 10; i++) {
            Wall.walls.add(new Square(i + 40 * i, 100, 40));
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //背景色
        g.fillRect(0, 0, WIDTH, HEIGHT);
        hero.setSpeed(1);
        synchronized (tanks) {
            for (int i = 0; i < tanks.size(); i++) {
                Tank enemy = tanks.get(i);
                enemy.draw(g);
            }
        }
        synchronized (Tank.bullets) {
            //防止读取size后
            //其他线程修改了bullets
            for (int i = 0; i < Tank.bullets.size(); i++) {
                Tank.Bullet bullet = Tank.bullets.get(i);
                bullet.draw(g);
                //已生成的炮弹draw函数
            }
        }
        synchronized (Wall.walls) {
            for (int i = 0; i < Wall.walls.size(); i++) {
                Wall.walls.get(i).draw(g);
            }
        }
        synchronized (Tank.Boom.booms) {
            for (int i = 0; i < Tank.Boom.booms.size(); i++) {
                Tank.Boom.booms.get(i).draw(g);
            }
        }
        new Thread(this).start();
        //启用一个线程不断重绘
//        try {
//            Thread.sleep(1000/144);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP ||
                e.getKeyCode() == KeyEvent.VK_W) {
            hero.moveUp();
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN ||
                e.getKeyCode() == KeyEvent.VK_S) {
            hero.moveDown();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT ||
                e.getKeyCode() == KeyEvent.VK_A) {
            hero.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT ||
                e.getKeyCode() == KeyEvent.VK_D) {
            hero.moveRight();
        }

        if (e.getKeyCode() == KeyEvent.VK_J) {
            //发射炮弹
            //每次攻击都生成一个炮弹类放进炮弹列表
            hero.fire();
        }
        if (e.getKeyCode() == KeyEvent.VK_K) {
            hero.setX(hero.getDefaultX());
            hero.setY(hero.getDefaultY());
            hero.setDirect(hero.getDefaultDirect());
            tanks.add(hero);
        }

        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP ||
                e.getKeyCode() == KeyEvent.VK_W) {
            hero.stopMoveUp();
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN ||
                e.getKeyCode() == KeyEvent.VK_S) {
            hero.stopMoveDown();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT ||
                e.getKeyCode() == KeyEvent.VK_A) {
            hero.stopMoveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT ||
                e.getKeyCode() == KeyEvent.VK_D) {
            hero.stopMoveRight();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.stopFire();
        }
    }

    @Override
    public void run() {
        //启动一个线程负责重绘
        try {
            Thread.sleep(1000 / 144);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        repaint();
    }
}

